package com.zhongmai.yzfgrabanorder.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zhongmai.yzfgrabanorder.R;
import com.zhongmai.yzfgrabanorder.utils.Constants;
import com.zhongmai.yzfgrabanorder.utils.EncryptUtils;
import com.zhongmai.yzfgrabanorder.utils.HttpXUtils;
import com.zhongmai.yzfgrabanorder.utils.LogUtils;
import com.zhongmai.yzfgrabanorder.utils.MD5Util;
import com.zhongmai.yzfgrabanorder.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;

import static com.zhongmai.yzfgrabanorder.utils.Constants.user;
import static com.zhongmai.yzfgrabanorder.utils.LogUtils.showToast;

/**
 * 项目名称：YZFGrabAnOrder
 * 类描述：URL配置Dialog
 * 创建人：luohao
 * 创建时间：2021/5/29 13:57
 * 修改人：luohao
 * 修改时间：2021/5/29 13:57
 * 修改备注：
 */
public class LinkDialog extends Dialog implements View.OnClickListener {
    private final boolean isClose;
    private final URLSuccess urlSuccess;
    Context mContext;
    private EditText mET_url;
    private Button mBT_sure;


    public LinkDialog(Context context, boolean isClose, URLSuccess urlSuccess) {
        super(context);
        this.mContext = context;
        this.isClose = isClose;
        this.urlSuccess = urlSuccess;
        initView();
    }


    //初始化
    public void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_link, null);
        mET_url = (EditText) view.findViewById(R.id.mET_url);
        mBT_sure = (Button) view.findViewById(R.id.mBT_sure);
        mBT_sure.setOnClickListener(this);
        if (!TextUtils.isEmpty(Constants.Url)) {
            mET_url.setText(Constants.Url);
        }
        mET_url.setSelection(getEditText().length());
        super.setContentView(view);
        this.setCancelable(isClose);
        this.setCanceledOnTouchOutside(isClose);
    }


    //获取当前输入框对象
    public String getEditText() {
        return mET_url.getText().toString().trim();
    }


    @Override
    public void onClick(View v) {
        final String url = mET_url.getText().toString().trim();
        if (TextUtils.isEmpty(url)) {
            return;
        } else {
            if (TextUtils.isEmpty(user)) {
                LogUtils.bugErr("user=====null");

                if (TextUtils.isEmpty(user)) {
                    String phoneMark = Utils.getPhoneMark();
                    if (TextUtils.isEmpty(phoneMark)) {
                        showToast("无法获取设备信息...");
                        return;
                    } else if (phoneMark.contains("null")) {
                        showToast("请授予手机读取权限...");
                        return;
                    } else {
                        phoneMark = phoneMark.replaceAll(" ", "").trim();
                        LogUtils.bugErr("user---------" + phoneMark);
                        Constants.user = MD5Util.MD5_16(phoneMark);
                    }
                }
            }
            boolean b = urlLogin(url);
            if (b) {
                this.dismiss();
            }
        }
    }

    /**
     * 首次登录测试设备与URL
     *
     * @param url
     * @return
     */
    private boolean urlLogin(final String url) {
        final boolean[] isLink = {false};
        RequestParams params = new RequestParams(url + Constants.loginAddress);
        JSONObject json = new JSONObject();
        try {
            json.put("user", user);
            final String loginInfo = json.toString();
            String def = null;
            def = EncryptUtils.encrypt(loginInfo, Constants.encrypt_key, Constants.encrypt_iv);
            String mdf = EncryptUtils.md5(def + Constants.encrypt_key).toUpperCase();
            params.addBodyParameter("def", def);
            params.addParameter("mdf", mdf);
            HttpXUtils.xUtilsPostRequest(mContext, params, new HttpXUtils.MyCallback() {
                @Override
                public void myResult(String result) {
                    JSONObject jsonObject = null;
                    String code = null;
                    try {
                        jsonObject = new JSONObject(result);
                        code = jsonObject.getString("code");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (!TextUtils.isEmpty(code)) {
                        Constants.Url = url;
                        Utils.save2SP(mContext, Constants.sp_File, Constants.urlSPKey, url);
                    }
                    if (code.equals("100000")) {
                        isLink[0] = true;
                        urlSuccess.success();
                        dismiss();
                    } else if (code.equals("201012")) {
                        showToast("用户不存在或没有权限");
                        dismiss();
                    } else if (code.equals("204004")) {
                        showToast("用户已停用");
                        dismiss();
                    }
                }

                @Override
                public void onError(String err) {
                    showToast(err);
                }
            }, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isLink[0];
    }

    public interface URLSuccess {
        void success();
    }
}
